//
//  CYChangeBackGroundImageViewController.m
//  cyydt
//
//  Created by chu on 2019/2/15.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYChangeBackGroundImageViewController.h"
#import "CYChangeBackGroundImageCollectionViewCell.h"

@interface CYChangeBackGroundImageViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSArray *_images;
    NSIndexPath *_selIndexPath;
}
@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *cateArr;

@end

@implementation CYChangeBackGroundImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"背景图";
    _images = @[@"wd_gezl_bg", @"rezl_bg2", @"rezl_bg3", @"rezl_bg4", @"rezl_bg5", @"rezl_bg6", @"rezl_bg7", @"rezl_bg8"];
    _selIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.view addSubview:self.collectionView];
    [self rightBarItemWithTitle:@"保存"];
}

- (void)RighttouchEvent{
    NSInteger num = _selIndexPath.item + 1;
    [self editInfosWithParam:@{@"background":[NSNumber numberWithInteger:num]}];
}

#pragma mark - 修改用户信息
- (void)editInfosWithParam:(NSDictionary *)param{
    [EasyShowLodingView showLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"user/update"];
    [[XBRequest sharedInstance] postDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"修改用户信息 ---- %@",responseResult);
        [EasyShowLodingView hidenLoding];
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                [[UIApplication sharedApplication].keyWindow makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
                [[AppDelegate sharedAppDelegate] getUserInfosWithComplecation:^{
                    [[AppDelegate sharedAppDelegate] popViewController];
                }];
                
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _selIndexPath = indexPath;
    [collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CYChangeBackGroundImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYChangeBackGroundImageCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CYChangeBackGroundImageCollectionViewCell alloc] init];
    }
    cell.backImageView.image = [UIImage imageNamed:_images[indexPath.item]];
    if (_selIndexPath.item == indexPath.item) {
        cell.selImageView.hidden = NO;
    }else{
        cell.selImageView.hidden = YES;
    }
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kWindowW - 45) / 2, 87 * kWindowHOne);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(20, 15, 20, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 15;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    return view;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        //居左约束
        UICollectionViewFlowLayout *leftAlignedLayout = [[UICollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) collectionViewLayout:leftAlignedLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CYChangeBackGroundImageCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYChangeBackGroundImageCollectionViewCell"];
 
    }
    return _collectionView;
}

- (NSMutableArray *)cateArr{
    if (!_cateArr) {
        _cateArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _cateArr;
}

@end
